package com.zhihaoliang.coroutine.buss.net.data

import android.os.Handler
import android.os.Looper
import com.zhihaoliang.coroutine.buss.net.callback.IUserLoadData
import com.zhihaoliang.coroutine.buss.net.model.User
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext


/**
 * @author 支豪亮
 * @date 2022/10/13 17:50
 * @des 主页数据仓库
 */
class NetRepository {

    private val networkDataSource: NetworkDataSource = NetworkDataSource()

    private val handler: Handler = Handler(Looper.getMainLooper())

    /**
     * 线程 execute 阻塞
     */
    fun loadContributorsBlocking(iUserCallback: IUserLoadData) {
        Thread {
            val list: List<User> = networkDataSource.loadContributorsBlocking()
            handler.post {
                iUserCallback.onUserLoad(list)
            }
        }.start()
    }

    /**
     * 异步 enqueue 回调
     */
    fun loadContributorsCallbacks(userLoadData: IUserLoadData) {
        networkDataSource.loadContributorsCallbacks(userLoadData)
    }

    /**
     * 协成加载
     */
    suspend fun loadContributorsSuspend(userLoadData: IUserLoadData) {
        networkDataSource.loadContributorsSuspend(userLoadData)
    }

    /**
     * 协成并行
     */
    suspend fun loadContributorsConcurrent(userLoadData: IUserLoadData) {
        networkDataSource.loadContributorsConcurrent(userLoadData)
    }

    /**
     * 协成加载进度
     */
    suspend fun loadContributorsProgress(userLoadData: IUserLoadData) {
        networkDataSource.loadContributorsProgress(userLoadData)
    }

    /**
     * 协成通道
     */
    suspend fun loadContributorsChannels(userLoadData: IUserLoadData) {
        networkDataSource.loadContributorsChannels(userLoadData)
    }

}